package org.kd.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kd.util.JsonReturnData;
import org.kd.util.ReadPropertyPlaceholder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

public class BaseController {
	@Autowired
	protected ResponseAPI responseAPI;
	@Autowired
	protected ReadPropertyPlaceholder readPropertyPlaceholder;

	public static final String SUCCESS = "success";
	public static final String ERROR = "error";
	public static final String ERRORCODE = "500";

	/**
	 * @功能: 所有的子类方法执行之前都要先执行此方法，子类方法不需要在model此方法中的参数  aop
	 * @作者: 柯栋 @代号:隐无为
	 * @param model
	 */
	@ModelAttribute
	public void init(Model model) {
		// model.addAttribute("webPath", "http://127.0.0.1:8080");
		// model.addAttribute("imgPath", "http://127.0.0.1:8080/local/file/upload/");
		model.addAttribute("webPath", readPropertyPlaceholder.getProperty("webPath"));
		model.addAttribute("imgPath", readPropertyPlaceholder.getProperty("imgPath"));
		/*model.addAttribute("webPath", "http://localhost:8080/ssm-demo");
	    model.addAttribute("imgPath", "/local/upload");*/
	}

	/**@功能: 异常统一捕获
	 * @作者: 柯栋 @代号:隐无为
	 * @时间: 2017年2月28日
	 */
	@ExceptionHandler
	@ResponseBody
	public String exceptionProcess(HttpServletRequest request, HttpServletResponse response, RuntimeException ex) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<String>(500, "系统出错");
		ex.printStackTrace();
		return responseAPI.getJsonString(jsonReturnData);
	}

}
